package com.System.servlet;

import com.System.pojo.*;
import com.System.service.User;
import com.System.service.UserImpl.UserService;
import com.System.untils.SaltMD5Util;
import com.System.untils.SqlSessionFactoryUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/User/*")
public class UserServlet extends BaseServlet {
    private User service=new UserService();
    public void selectFocusBar(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        com.System.pojo.User user=new com.System.pojo.User(username,null);
        List<User_Bar_Relationship> userBarRelationship = service.selectFocusRelation(user);
        for (User_Bar_Relationship user_bar_relationship : userBarRelationship) {
            Bar bar = service.selectFocus(user_bar_relationship);
            // 设置日期格式化样式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 将 bar 对象转换为 JSONObject

            // 格式化创建时间并添加到 JSONObject 中
            String createdTime = dateFormat.format(bar.getCreated_at());
            bar.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(bar);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void selectBar(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Bar bar = JSON.parseObject(param, Bar.class);
        String barName = bar.getBarName();

        User_Bar_Relationship userBarRelationship = service.selectRelationship(new User_Bar_Relationship(username, barName));
        if(userBarRelationship!=null){
        int flag = userBarRelationship.getRelationShip();

        if(flag==0){
            String ref="http://localhost:8080/BarSystem/Bar.html?username="+username+"&barName="+barName+"";
            String jsonString = JSON.toJSONString(ref);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
        else if(flag==1){
            String redirectUrl = "http://localhost:8080/BarSystem/BarOwner.html?username=" + username + "&barName=" + barName;
            String jsonString = JSON.toJSONString(redirectUrl);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
//

        }
        }else{
            String ref="http://localhost:8080/BarSystem/Bar.html?username="+username+"&barName="+barName+"";
            String jsonString = JSON.toJSONString(ref);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void goToThisBar(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String barName = request.getParameter("barName");

        User_Bar_Relationship userBarRelationship = service.selectRelationship(new User_Bar_Relationship(username, barName));
        int flag = userBarRelationship.getRelationShip();

        if(flag==0){
            String ref="http://localhost:8080/BarSystem/Bar.html?username="+username+"&barName="+barName+"";
            String jsonString = JSON.toJSONString(ref);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
        else if(flag==1){
            String redirectUrl = "http://localhost:8080/BarSystem/BarOwner.html?username=" + username + "&barName=" + barName;
            String jsonString = JSON.toJSONString(redirectUrl);
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
//

        }
    }
    public void addBar(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        BufferedReader reader = request.getReader();
        String param = reader.readLine();
        Bar bar = JSON.parseObject(param, Bar.class);
        bar.setOwnerName(username);
        Bar result = service.selectBar(bar);
        if(result!=null){
            response.getWriter().write("BarName has exit");
        }else {
            service.addBar(bar);
            service.addRelationship(new User_Bar_Relationship(username, bar.getBarName(),1));
            response.getWriter().write("success");
        }
    }

    public void selectAllLikeComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        com.System.pojo.User user=new com.System.pojo.User(username,null);
        List<Support> supports = service.selectAllMySupport(user);
        for (Support  support : supports) {
            Comment comment = service.selectMyLikeComment(support);
            // 设置日期格式化样式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 将 bar 对象转换为 JSONObject

            // 格式化创建时间并添加到 JSONObject 中
            String createdTime = dateFormat.format(comment.getCreated_at());
            comment.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(comment);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }

    public void selectHotBars(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Bar> bars = service.selectHotPosts();
        for (Bar bar : bars) {
            // 设置日期格式化样式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 将 bar 对象转换为 JSONObject

            // 格式化创建时间并添加到 JSONObject 中
            String createdTime = dateFormat.format(bar.getCreated_at());
            bar.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(bar);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }
    public void selectAllPosts(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Post> posts = service.selectAllPosts();
        for (Post post:posts) {
            // 设置日期格式化样式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 将 bar 对象转换为 JSONObject

            // 格式化创建时间并添加到 JSONObject 中
            String createdTime = dateFormat.format(post.getCreated_at());
            post.setCreated_at(null);
            JSONObject barObject = (JSONObject) JSON.toJSON(post);
            barObject.put("created_at", createdTime);

            // 将整合后的 JSONObject 转换为字符串输出
            String jsonString = barObject.toJSONString();

            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(jsonString);
        }
    }

    }
